package org.come.Jpanel;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.tool.btn.GoodPanelBtn;
import org.come.entity.Goodstable;
import org.come.mouslisten.RefinersMouslisten;
import org.come.mouslisten.WLLMouslisten;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.Goodtype;
import org.come.until.Util;

import com.tool.btn.RefineOperBtn;
import com.tool.btn.goodbtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**
 * @author Administrator 炼器
 */
public class RefinersJpanel extends JPanel {

    public BigDecimal money = new BigDecimal(1000000);// 消耗金钱
    private RefineOperBtn workshopBtn;// 炼器按钮
    private GoodPanelBtn clearAll; //当前整理背包
    private JLabel labOwner,// 作坊主
            labSegment,// 段位
            labLevel,// 等级
            labShulian,// 熟练度
            labAchieve;// 成就

    public JLabel[] labMark = new JLabel[5];// 放置炼化装备以及炼化材料
    public Goodstable[] goods = new Goodstable[5];
    public JLabel[] GoodsListLabel = new JLabel[24]; // 用来存放24个物品表格
    // 放置6个背包切换按钮
    private goodbtn[] btnrights;

    public RefinersJpanel() {
        if ("水墨UI".equals(MyIsif.getStyle())) {
            this.setPreferredSize(new Dimension(558, 516));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 炼器按钮
            workshopBtn = new RefineOperBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, this, 0,
                    "?");
            workshopBtn.setBounds(448, 125, 59, 24);
            this.add(workshopBtn);
            // 放置炼化装备以及炼化材料
            for (int i = 0; i < 5; i++) {
                labMark[i] = new JLabel();
                if (i == 4) {
                    labMark[i].setBounds(385, 112, 53, 51);
                } else {
                    int row = i % 2;
                    int col = i / 2;
                    labMark[i].setBounds(250 + row * 67, 79 + col * 65, 53, 51);
                }
                labMark[i].addMouseListener(new RefinersMouslisten(this, i + 24));
                this.add(labMark[i]);
            }

            Font font = new Font("宋体", 0, 14);
            Color color = new Color(187, 165, 75);
            // 作坊主
            labOwner = new JLabel();
            labOwner.setBounds(88, 312, 96, 14);
            labOwner.setForeground(color);
            labOwner.setFont(font);
            this.add(labOwner);
            // 段位
            labSegment = new JLabel();
            labSegment.setBounds(88, 344, 96, 14);
            labSegment.setForeground(color);
            labSegment.setFont(font);
            this.add(labSegment);
            // 等级
            labLevel = new JLabel();
            labLevel.setBounds(88, 376, 96, 14);
            labLevel.setForeground(color);
            labLevel.setFont(font);
            this.add(labLevel);
            // 熟练度
            labShulian = new JLabel();
            labShulian.setBounds(88, 408, 96, 14);
            labShulian.setForeground(color);
            labShulian.setFont(font);
            this.add(labShulian);
            // 成就
            labAchieve = new JLabel();
            labAchieve.setBounds(88, 440, 96, 14);
            labAchieve.setForeground(color);
            labAchieve.setFont(font);
            this.add(labAchieve);
            // 存放物品的表格1~24
            for (int i = 0; i < 24; i++) {
                int row = i % 6;
                int col = i / 6;
                GoodsListLabel[i] = new JLabel();
                GoodsListLabel[i].addMouseListener(new RefinersMouslisten(this, i));
                GoodsListLabel[i].setBounds(218 + row * 51, 277 + col * 51, 49, 49);
                this.add(GoodsListLabel[i]);
            }
            // 背包四个页数的按钮
            btnrights = new goodbtn[6];
            for (int i = 0; i < btnrights.length; i++) {
                btnrights[i] = new goodbtn("skin/8100226/newbutton/C0" + (i + 1)+".png", 0, this, i);
                btnrights[i].setBounds(524, 284 + i * 35, 35, 31);
                this.add(btnrights[i]);
            }

            this.labGoodstxt = new JLabel[6];//这两行添加作坊炼化提示显示，这个只是水墨界面--------小情调备注
            this.redis();

        } else {
            this.setPreferredSize(new Dimension(536, 542));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);
            // 炼器按钮

            //当前整理背包
            clearAll = new GoodPanelBtn("inkImg/hongmu/1/a3.png", 1, "整");
            clearAll.addMouseListener(new WLLMouslisten(17));
            clearAll.setBounds(188 - 20, 205 + 85, 17, 17);
            this.add(clearAll);

            workshopBtn = new RefineOperBtn("inkImg/hongmu/1/w50.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, this, 0,
                    "?");
            workshopBtn.setBounds(421, 125, 59, 26);
            this.add(workshopBtn);
            // 放置炼化装备以及炼化材料
            for (int i = 0; i < 5; i++) {
                labMark[i] = new JLabel();
                if (i == 4) {
                    labMark[i].setBounds(361, 132, 53, 51);
                } else {
                    int row = i % 2;
                    int col = i / 2;
                    labMark[i].setBounds(226 + row * 67, 99 + col * 65, 54, 51);
                }
                labMark[i].addMouseListener(new RefinersMouslisten(this, i + 24));
                this.add(labMark[i]);
            }
            Font font = new Font("宋体", 0, 14);
            Color color = new Color(187, 165, 75);
            // 作坊主
            labOwner = new JLabel();
            labOwner.setBounds(88, 312, 96, 14);
            labOwner.setForeground(color);
            labOwner.setFont(font);
            this.add(labOwner);
            // 段位
            labSegment = new JLabel();
            labSegment.setBounds(88, 344, 96, 14);
            labSegment.setForeground(color);
            labSegment.setFont(font);
            this.add(labSegment);
            // 等级
            labLevel = new JLabel();
            labLevel.setBounds(88, 376, 96, 14);
            labLevel.setForeground(color);
            labLevel.setFont(font);
            this.add(labLevel);
            // 熟练度
            labShulian = new JLabel();
            labShulian.setBounds(88, 408, 96, 14);
            labShulian.setForeground(color);
            labShulian.setFont(font);
            this.add(labShulian);
            // 成就
            labAchieve = new JLabel();
            labAchieve.setBounds(88, 440, 96, 14);
            labAchieve.setForeground(color);
            labAchieve.setFont(font);
            this.add(labAchieve);
            // 存放物品的表格1~24
            for (int i = 0; i < 24; i++) {
                int row = i % 6;
                int col = i / 6;
                GoodsListLabel[i] = new JLabel();
                GoodsListLabel[i].addMouseListener(new RefinersMouslisten(this, i));
                GoodsListLabel[i].setBounds(200 + row * 51, 287 + col * 51, 49, 49);
                this.add(GoodsListLabel[i]);
            }
            // 背包六个页数的按钮
            btnrights = new goodbtn[6];
            for (int i = 0; i < btnrights.length; i++) {
                btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);
                if (i == 1) {
                    this.btnrights[i].setBounds(81 + 161, 442 + 60, 50, 24);//包裹2
                } else if (i == 2) {
                    btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);//包裹3
                    this.btnrights[i].setBounds(132 + 160, 442 + 52, 50, 40);
                } else if (i == 3) {
                    btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);//包裹4
                    this.btnrights[i].setBounds(183 + 160, 442 + 60, 50, 24);
                } else if (i == 4) {
                    btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);//包裹5
                    this.btnrights[i].setBounds(183 + 210, 442 + 60, 50, 24);
                } else if (i == 5) {
                    btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);//包裹6
                    this.btnrights[i].setBounds(234 + 210, 442 + 60, 50, 24);
                } else {
                    btnrights[i] = new goodbtn("inkImg/hongmu/1/50x51.png", 0, this, i);//包裹1
                    this.btnrights[i].setBounds(28 + 162, 442 + 51, 50, 40);
                }
                this.add(this.btnrights[i]);
            }
            this.labGoodstxt = new JLabel[6];//这两行添加作坊炼化提示显示，这个只是水墨界面--------小情调备注
            this.redis2();
        }
    }
    private JLabel[] labGoodstxt;
    /**
     * 点击good
     */
    public void redis(){


        Font font = new Font("宋体", Font.PLAIN, 15);
        Color color = new Color(239, 238, 12);
        // 炼器水墨
        JLabel labkaiguang = new JLabel("开光");
        labkaiguang.setBounds(55, 72, 141, 20);
        labkaiguang.setForeground(color);
        labkaiguang.setFont(font);
        this.add(labkaiguang);
        JLabel lablianqi = new JLabel("炼器");
        lablianqi.setBounds(55, 92, 141, 20);
        lablianqi.setForeground(color);
        lablianqi.setFont(font);
        this.add(lablianqi);
        JLabel labqclq = new JLabel("清除炼器");
        labqclq.setBounds(55, 112, 141, 20);
        labqclq.setForeground(color);
        labqclq.setFont(font);
        this.add(labqclq);

        labkaiguang.addMouseListener(new MouseAdapter(){

            @Override
            public void mousePressed(final MouseEvent e){
                labkaiguang.setOpaque(true);
                labkaiguang.setBackground(new Color(33, 42, 52));
                lablianqi.setOpaque(false);
                lablianqi.setBackground(null);
                labqclq.setBackground(null);
                labqclq.setOpaque(false);
                RefinersJpanel.this.labGoodstxt[0].setText("武器");
                RefinersJpanel.this.labGoodstxt[1].setText("九天玄玉");
                RefinersJpanel.this.labGoodstxt[2].setText("--");
                RefinersJpanel.this.labGoodstxt[3].setText("--");
                RefinersJpanel.this.labGoodstxt[4].setText("--");
                RefinersJpanel.this.labGoodstxt[5].setText("--");
            }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
        });

        lablianqi.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(final MouseEvent e){
                labkaiguang.setBackground(null);
                labkaiguang.setOpaque(false);
                lablianqi.setOpaque(true);
                lablianqi.setBackground(new Color(33, 42, 52));
                labqclq.setBackground(null);
                labqclq.setOpaque(false);
                RefinersJpanel.this.labGoodstxt[0].setText("武器");
                RefinersJpanel.this.labGoodstxt[1].setText("落魄沙");
                RefinersJpanel.this.labGoodstxt[2].setText("落魄沙");
                RefinersJpanel.this.labGoodstxt[3].setText("落魄沙");
                RefinersJpanel.this.labGoodstxt[4].setText("--");
                RefinersJpanel.this.labGoodstxt[5].setText("--");
            }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
        });
        labqclq.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(final MouseEvent e){
                labkaiguang.setOpaque(false);
                labkaiguang.setBackground(null);
                lablianqi.setOpaque(false);
                lablianqi.setBackground(null);
                labqclq.setOpaque(true);
                labqclq.setBackground(new Color(33, 42, 52));
                RefinersJpanel.this.labGoodstxt[0].setText("武器");
                RefinersJpanel.this.labGoodstxt[1].setText("--");
                RefinersJpanel.this.labGoodstxt[2].setText("--");
                RefinersJpanel.this.labGoodstxt[3].setText("--");
                RefinersJpanel.this.labGoodstxt[4].setText("--");
                RefinersJpanel.this.labGoodstxt[5].setText("--");
            }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
        });

        int i = 0;
        while (i < 6) {
            final int row = i % 3;
            final int col = i / 3;
            (this.labGoodstxt[i] = new JLabel("", 0)).setBounds(246 + row * 67, 76 + col * 65, 54, 51);
            this.labGoodstxt[i].setForeground(new Color(102, 102, 102));
            this.labGoodstxt[i].setFont(new Font("宋体", 0, 13));
            this.add((Component)this.labGoodstxt[i]);
            ++i;
        }
    }


        public void redis2(){

        Font font = new Font("宋体", Font.PLAIN, 15);
        Color color = new Color(239, 238, 12);
        // 炼器红木
        JLabel labkaiguang = new JLabel("开光");
        labkaiguang.setBounds(29, 85, 141, 20);
        labkaiguang.setForeground(color);
        labkaiguang.setFont(font);
        this.add(labkaiguang);
        JLabel lablianqi = new JLabel("炼器");
        lablianqi.setBounds(29, 105, 141, 20);
        lablianqi.setForeground(color);
        lablianqi.setFont(font);
        this.add(lablianqi);
        JLabel labqclq = new JLabel("清除炼器");
        labqclq.setBounds(29, 125, 141, 20);
        labqclq.setForeground(color);
        labqclq.setFont(font);
        this.add(labqclq);

            labkaiguang.addMouseListener(new MouseAdapter(){

                @Override
                public void mousePressed(final MouseEvent e){
                labkaiguang.setOpaque(true);
                labkaiguang.setBackground(new Color(33, 42, 52));
                lablianqi.setOpaque(false);
                lablianqi.setBackground(null);
                labqclq.setBackground(null);
                labqclq.setOpaque(false);
                            RefinersJpanel.this.labGoodstxt[0].setText("武器");
                            RefinersJpanel.this.labGoodstxt[1].setText("九天玄玉");
                            RefinersJpanel.this.labGoodstxt[2].setText("--");
                            RefinersJpanel.this.labGoodstxt[3].setText("--");
                            RefinersJpanel.this.labGoodstxt[4].setText("--");
                            RefinersJpanel.this.labGoodstxt[5].setText("--");
                }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
            });

        lablianqi.addMouseListener(new MouseAdapter(){

                            @Override
                            public void mousePressed(final MouseEvent e){
                labkaiguang.setBackground(null);
                labkaiguang.setOpaque(false);
                lablianqi.setOpaque(true);
                lablianqi.setBackground(new Color(33, 42, 52));
                labqclq.setBackground(null);
                labqclq.setOpaque(false);
                                RefinersJpanel.this.labGoodstxt[0].setText("武器");
                                RefinersJpanel.this.labGoodstxt[1].setText("落魄沙");
                                RefinersJpanel.this.labGoodstxt[2].setText("落魄沙");
                                RefinersJpanel.this.labGoodstxt[3].setText("落魄沙");
                                RefinersJpanel.this.labGoodstxt[4].setText("--");
                                RefinersJpanel.this.labGoodstxt[5].setText("--");
                            }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
        });
        labqclq.addMouseListener(new MouseAdapter(){
            @Override
            public void mousePressed(final MouseEvent e){
                labkaiguang.setOpaque(false);
                labkaiguang.setBackground(null);
                lablianqi.setOpaque(false);
                lablianqi.setBackground(null);
                labqclq.setOpaque(true);
                labqclq.setBackground(new Color(33, 42, 52));
                RefinersJpanel.this.labGoodstxt[0].setText("武器");
                RefinersJpanel.this.labGoodstxt[1].setText("--");
                RefinersJpanel.this.labGoodstxt[2].setText("--");
                RefinersJpanel.this.labGoodstxt[3].setText("--");
                RefinersJpanel.this.labGoodstxt[4].setText("--");
                RefinersJpanel.this.labGoodstxt[5].setText("--");
            }
           /* @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                labZbwz.setBackground(null);
                labZbwz.setOpaque(false);
            }*/
        });

        int i = 0;
        while (i < 6) {
            final int row = i % 3;
            final int col = i / 3;
            (this.labGoodstxt[i] = new JLabel("", 0)).setBounds(246 + row * 67, 76 + col * 65, 54, 51);
            this.labGoodstxt[i].setForeground(new Color(102, 102, 102));
            this.labGoodstxt[i].setFont(new Font("宋体", 0, 13));
            this.add((Component)this.labGoodstxt[i]);
            ++i;
        }
    }

    public void ClickGood(Goodstable good, int path) {
        if (path < 24) {
            if (Goodtype.Weapons(good.getType())) {
                change(good, 0);
            } else {
                if (good.getType() == 505 || good.getType() == 507) {
                    path = vacantGood();
                    if (path > 0) {
                        change(good, path);
                    }
                }
            }

        } else {
            change(null, path - 24);
        }
        String v = detection();
        if (!workshopBtn.getText().equals(v)) {
            workshopBtn.setText(v);
        }
    }

    /**
     * 重置物品
     */
    public void ResetGood(Goodstable good) {
        for (int i = 0; i < goods.length; i++) {
            if (goods[i] != null && goods[i].getRgid().compareTo(good.getRgid()) == 0) {
                if (good.getUsetime() <= 0) {
                    goods[i] = null;
                    labMark[i].setIcon(null);
                } else {
                    goods[i] = good;
                    labMark[i].setIcon(GoodsListFromServerUntil.imgpathAdaptive(good.getSkin(), 49, 49));
                }
                String v = detection();
                if (!workshopBtn.getText().equals(v)) {
                    workshopBtn.setText(v);
                }
                break;
            }
        }
    }

    /**
     * 检测公式
     */
    public String detection() {
        // 检测是否是清除炼器
        int path = vacantGood();
        if (path == 0) {
            return "?";
        }
        Goodstable good = goods[0];
        if (!Goodtype.Weapons(good.getType())) {
            return "?";
        }
        if (path == 1) {
            if (good.getValue().indexOf("炼器属性") != -1) {
                return "清除";
            }
        } else if (path == 2) {
            if (goods[1].getType() == 505) {
                return "开光";
            }
        } else if (path == 4) {
            if (goods[1].getType() == 507 || goods[2].getType() == 507 || goods[3].getType() == 507) {
                return "炼器";
            }
        }
        return "?";
    }

    /**
     * 切换指定位置
     */
    public void change(Goodstable good, int path) {
        goods[path] = good;
        if (good != null) {
            labMark[path].setIcon(GoodsListFromServerUntil.imgpathAdaptive(good.getSkin(), 49, 49));
        } else {
            labMark[path].setIcon(null);
        }
    }

    /**
     * 返回空余位置
     */
    public int vacantGood() {
        for (int i = 0; i < goods.length; i++) {
            if (goods[i] == null) {
                return i;
            }
        }
        return -1;
    }

    private ImageIcon icon;
    private ImageIcon iconSixGoods;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if ("水墨UI".equals(MyIsif.getStyle())) {
            if (icon == null) {
                icon = CutButtonImage.getImage("inkImg/newbackground/B238.png", -1, -1);
            }
            // 背景
            g.drawImage(icon.getImage(), 0, 0, this);
            if (iconSixGoods == null) {
                iconSixGoods = new ImageIcon("inkImg/newbackground/B248.png");
            }

            g.drawImage(iconSixGoods.getImage(), 214, 55, this);
            // 画物品的图片和数量
            GoodsListFromServerUntil.draw(g, 218, 287);
            // 画现金
            Util.drawMoney(g, 322, 231);
            // 画消耗金钱
            if (money != null) {
                Util.drawPrice(g, money, 322, 258);
            }
        } else {
            if (this.icon == null) {
                this.icon = new ImageIcon("inkImg/hongmu/2/worhshop_lq.png");
            }

            g.drawImage(this.icon.getImage(), 0, 0, this);
            GoodsListFromServerUntil.draw(g, 193, 298);
            Util.drawMoney(g, 297, 242);
            Util.drawMoneyS(g, 87, 325);
            if (this.money != null) {
                Util.drawPrice(g, this.money, 297, 269);
            }
        }
    }

    public goodbtn[] getBtnrights() {
        return btnrights;
    }

    public void setBtnrights(goodbtn[] btnrights) {
        this.btnrights = btnrights;
    }

}
